package com.pickgliss.ui.controls.container
{
	import com.pickgliss.effect.IEffect;
	import com.pickgliss.events.ComponentEvent;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.core.Disposeable;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	/**
	 * 
	 * @author Herman
	 * 一个支持简单排列的容器
	 * 
	 */	
	public class BoxContainer extends Component
	{
		public static const P_childRefresh:String="childRefresh";
		public static const P_childResize:String = "childResize";
		public static const P_isReverAdd:String = "isReverAdd";
		public static const P_spacing:String = "spacing";
		public static const P_strictSize:String = "strictSize";
		public static const P_autoSize:String = "autoSize";
		
		public static const LEFT_OR_TOP:int = 0;
		public static const RIGHT_OR_BOTTOM:int = 1;
		public static const CENTER:int = 2;
		
		public function BoxContainer()
		{
			_childrenList = new Vector.<DisplayObject>();
			super();
		}

		protected var _childrenList:Vector.<DisplayObject>;
		protected var _isReverAdd:Boolean;
		protected var _spacing:Number = 0;
		protected var _strictSize:Number = -1;
		protected var _autoSize:int = LEFT_OR_TOP;

		override public function addChild(child:DisplayObject):DisplayObject
		{
			if(!_isReverAdd)
			{
				_childrenList.push(super.addChild(child));
			}else
			{
				_childrenList.push(super.addChildAt(child,0));
			}
			child.addEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			onPropertiesChanged(P_childRefresh);
			return child;
		}

		override public function dispose():void
		{
			disposeAllChildren();
			_childrenList = null;
			super.dispose();
		}
		/**
		 * 释放所有子对象 
		 * 
		 */		
		public function disposeAllChildren():void
		{
			for(var i:int = 0;i<numChildren;i++)
			{
				var child:DisplayObject = getChildAt(i);
				child.removeEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			}
			ObjectUtils.disposeAllChildren(this);
		}
		public function set isReverAdd(value:Boolean):void
		{
			if(_isReverAdd == value)return;
			_isReverAdd = value;
			onPropertiesChanged(P_isReverAdd);
		}
		public function refreshChildPos():void
		{
			onPropertiesChanged(P_childResize);
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			child.removeEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			_childrenList.splice(_childrenList.indexOf(child),1);
			super.removeChild(child);
			onPropertiesChanged(P_childRefresh);
			return child;
		}
		
		public function reverChildren():void
		{
			var tempAllChildren:Vector.<DisplayObject> = new Vector.<DisplayObject>();
			while (numChildren>0)
			{
				tempAllChildren.push(removeChildAt(0));
			}
			for (var i:int = 0; i<tempAllChildren.length; i++)
			{
				addChild(tempAllChildren[i]);
			}
		}
		
		public function set autoSize(value:int):void
		{
			if(_autoSize == value)return;
			_autoSize = value;
			onPropertiesChanged(P_autoSize);
		}
		
		public function get spacing():Number
		{
			return _spacing;
		}

		/**
		 * 
		 * @param value 间距
		 * 当是VBox对象时此值表示的是纵向的间距
		 * 当是HBox对象时此值表示的是横向的间距
		 * 
		 */			
		public function set spacing(value:Number):void
		{
			if(_spacing == value) return;
			_spacing = value;
			onPropertiesChanged(P_spacing);
		}
		
		public function set strictSize(value:Number):void
		{
			if(_strictSize == value)return;
			_strictSize = value;
			onPropertiesChanged(P_strictSize);
		}
		/**
		 *执行排列的方法 
		 * 
		 */			
		protected function arrange():void
		{
			
		}
		
		protected function get isStrictSize():Boolean
		{
			return _strictSize > 0;
		}

		override protected function onProppertiesUpdate():void
		{
			arrange();
		}
				
		private function __onResize(event:ComponentEvent):void
		{
			if (event.changedProperties[Component.P_height] || event.changedProperties[Component.P_width])
			{
				onPropertiesChanged(P_childRefresh);
			}
		}
	}
}